package com.symria.framework.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.symria.framework.constant.ATTFrameWork;
import com.symria.role.util.Logger;
import com.symria.server.bo.role.RoleService;
import com.symria.server.po.AuthInfo;
import com.symria.server.po.MenuInfo;
import com.symria.server.po.RoleInfo;

public class FrameCache {
	private static Logger logger = Logger.getLogger(FrameCache.class);
	private static FrameCache frameCache = null;
	private static Map<String, List<MenuInfo>> menuCache = new HashMap<String, List<MenuInfo>>();//角色菜单
	private static Map<String, List<AuthInfo>> authCache = new HashMap<String, List<AuthInfo>>();//角色菜单
    public static boolean cache=false;
    public static RoleService roleService;
    
    public synchronized static FrameCache getInstance() {
    	if(frameCache==null){
    		frameCache = new FrameCache();
    		roleService = SpringUtil.getBean("roleService", RoleService.class);
    	}
    	return frameCache;
    }
    
    public static void initCache(){
    	if(!cache){
    		cacheMenuCache();
    		cacheAuthCache();
    		cache=true;
    	}
    
    }
    
    //缓存角色菜单
    public static void cacheMenuCache(){
    	logger.logInfo("begin cache menu.......");
    	List<RoleInfo> list = roleService.queryRoleInfo();
    	List<MenuInfo> ml = roleService.queryRootMenu();
    	for(RoleInfo role:list){
    		List<MenuInfo> l = roleService.queryMenuInfoByRoleId(null,role.getRoleId());
    		String regexp = createRegexp(l);    		
    		menuCache.put(role.getRoleId()+"", filterMenuInfoByRole(ml,regexp));
    	}
    	
    	
    	logger.logInfo(" done ! cache menu.......");
    }
    
    //缓存角色权限
    public static void cacheAuthCache(){
    	logger.logInfo("begin cache auth......");
    	List<RoleInfo> list = roleService.queryRoleInfo();
    	for(RoleInfo role:list){
    		List<AuthInfo> l = roleService.queryAuthInfoByRoleId(role.getRoleId());
    		authCache.put(role.getRoleId()+"", l);
    	}
    	logger.logInfo("done cache auth......");
    }
    private static List<MenuInfo> filterMenuInfoByRole(List<MenuInfo> fullList,String regexp){
    	List<MenuInfo> list = new ArrayList<MenuInfo>();
    	for(MenuInfo m:fullList){
    		MenuInfo cm = new MenuInfo();
    		if(ATTFrameWork.LOGIC_TRUE.equals(m.getVisitility())&&FrameCache.regex(regexp,StringUtil.dealNull(m.getId()))){			
    			cm.setId(m.getId());
    			cm.setMenuDesc(m.getMenuDesc());
    			cm.setMenuHref(m.getMenuHref());
    			cm.setMenuLev(m.getMenuLev());
    			cm.setMenuName(m.getMenuName());
    			cm.setMenuSeq(m.getMenuSeq());
    			cm.setOwner(m.getOwner());
    			cm.setPId(m.getPId());
    			cm.setVisitility(m.getVisitility());
    			cm.setList(filterMenuInfoByRole(m.getList(),regexp));
    			list.add(cm);
    		}
    	}
    	return list;
    }

	private static boolean regex(String regex, String str) {
		boolean trueOrFalse;
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(str);
		trueOrFalse = m.matches();
		return trueOrFalse;
	}
    
    private static String createRegexp(List<MenuInfo> l){
    	StringBuffer sbuf=new StringBuffer("(-1");
    	for(MenuInfo m:l){
    		sbuf.append("|"+m.getId());
    	}
    	sbuf.append(")");
    	return sbuf.toString();
    }
    
    public List<MenuInfo> getMenuCacheByRole(Integer roleId){
    	initCache();
    	return menuCache.get(roleId+"");
    }
    public List<AuthInfo> getAuthInfoCacheByRole(Integer roleId){
    	initCache();
    	return authCache.get(roleId+"");
    }
    public Map<String, List<MenuInfo>> getMenuCache(){
    	initCache();
    	return menuCache;
    }
    public Map<String, List<AuthInfo>> getauthCache(){
    	initCache();
    	return authCache;
    }
    
}
